//
//  SDScrollTableViewCell.m
//  CarUser
//
//  Created by ayi on 2019/1/8.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "CustomSDScrollTableViewCell.h"
#import "SDPhotoBrowser.h"
@interface CustomSDScrollTableViewCell()<SDCycleScrollViewDelegate,SDPhotoBrowserDelegate>

@property (nonatomic, strong) UILabel *indexLab;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation CustomSDScrollTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+ (CustomSDScrollTableViewCell *)initCellWithReuseIdentifier:(NSString *)reuseIdentifier andTab:(UITableView *)tab{
    CustomSDScrollTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[CustomSDScrollTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _indexLab = [UILabel customInitLabWithFont:12 andTextColor:UIColor.whiteColor andAlign:NSTextAlignmentCenter];
        _indexLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        
        [self.contentView addSubview:self.adSDScrollview];
        [self.contentView addSubview:self.indexLab];
        
        [self.adSDScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        [self.indexLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-10);
            make.right.mas_equalTo(-10);
            make.height.mas_equalTo(ceilf(self.indexLab.font.lineHeight + 2));
            make.width.mas_equalTo(34);
        }];
//        [self.indexLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        
        self.indexLab.layer.masksToBounds = YES;
        self.indexLab.layer.cornerRadius = ceilf(self.indexLab.font.lineHeight + 2)/2;
        
        self.currentIndex = 1;
    }
    return self;
}

- (void)setListAry:(NSMutableArray *)listAry{
    _listAry = listAry;
    
    self.adSDScrollview.imageURLStringsGroup = listAry;
    if (listAry.count == 0) {
        self.indexLab.hidden = YES;
    }else{
        self.indexLab.hidden = NO;
        self.indexLab.text = [NSString stringWithFormat:@"%ld/%ld",self.currentIndex,self.listAry.count];
    }
    
}


//// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    
    return [NSURL URLWithString:self.listAry[index]];
}



/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    self.currentIndex = index + 1;
    self.indexLab.text = [NSString stringWithFormat:@"%ld/%ld",index + 1 ,self.listAry.count];
    
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = index;;
    photoBrowser.imageCount = self.listAry.count;
    //photoBrowser.sourceImagesContainerView = self.adSDScrollview;
    [photoBrowser show];
    
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    self.currentIndex = index + 1;
    self.indexLab.text = [NSString stringWithFormat:@"%ld/%ld",index + 1 ,self.listAry.count];
}


/*
 初始化
 */
- (SDCycleScrollView *)adSDScrollview{
    if (!_adSDScrollview) {
        _adSDScrollview = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 200) delegate:self placeholderImage:nil];
        _adSDScrollview.backgroundColor = [UIColor whiteColor];
        _adSDScrollview.hidesForSinglePage = YES;
        _adSDScrollview.pageControlDotSize = CGSizeMake(30, 5);
        _adSDScrollview.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _adSDScrollview.titleLabelHeight = 0;
        _adSDScrollview.autoScrollTimeInterval = 2;
        _adSDScrollview.showPageControl = NO;
        _adSDScrollview.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _adSDScrollview;
}


@end
